package com.chinasoft.mall.activity.subActivity.goods;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.BaseActivity;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.adapter.SpAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.GoodsClassBean;
import com.chinasoft.mall.widget.LoadListView;

public class GoodsActivity extends BaseActivity{
	
	private LoadListView modellist;
	private BaseBean sp_bb;
	private SpAdapter mlAdapter;
	private List<GoodsClassBean> gcbs;
	//将所属分类的商品全部显示出来
	private BaseBean goodsbyclass_bb;
	private String goodsbyclasstitle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sp_list);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		sp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_GOODSCLASS);
		if (sp_bb != null) {
			success(sp_bb);
		} else {
			sp_bb = new BaseBean(Config.GET_GOODSCLASS);
			control.sendMessage(sp_bb);
		}
	}

	private void initView() {
		modellist=(LoadListView) findViewById(R.id.modellist);
		mlAdapter=new SpAdapter((MainActivity)this.getParent(), null);
		modellist.setAdapter(mlAdapter);
		modellist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				GoodsClassBean scd = gcbs.get(position);
				if (scd != null) {
					goodsbyclasstitle=scd.getCLASS_NAME();
					goodsbyclass_bb = new BaseBean(Config.GET_GOODSBYCLASS);
					goodsbyclass_bb.reqData.put("class_id",  scd.getCLASS_ID());
					goodsbyclass_bb.reqData.put("pageNo",  "1");
					goodsbyclass_bb.reqData.put("pageSize",  "10");
					control.sendMessage(goodsbyclass_bb);
//					((MainActivity)GoodsActivity.this.getParent()).setCurrentActivity(GoodsListActivity.class,
//							goodsbyclasstitle, scd.getCLASS_ID());
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if(obj.equals(sp_bb)){
			gcbs=(List<GoodsClassBean>)obj.getResData().getData();
			mlAdapter.setData(gcbs);
		}
		else if(obj.equals(goodsbyclass_bb)){
			((MainActivity)this.getParent()).setCurrentActivity(GoodsListActivity.class,
					goodsbyclasstitle, obj);
		}
	}
}
